/*
 *  i386 emulator asynchronous debug tool 
 *
 *  Copyright (c) 2003-2005 Fabrice Bellard
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <string.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include "qemu-stats.h"

/* force a flush. does not return until the code cache is flushed */
#if 0
static void force_flush(REMOTE_CONTROL *smem)
{
   smem->force_flush = 1;
   while(smem->force_flush);
}
#endif

/* force a dump of current statistics. does not return until the statistics dumped */
static void force_dump(REMOTE_CONTROL *smem)
{
   smem->force_dump = 1;
   while(smem->force_dump);
}

/* force a reset of current statistics. does not return until the statistics dumped */
static void force_reset(REMOTE_CONTROL *smem)
{
   smem->force_reset = 1;
   while(smem->force_reset);
}

/* hook up the shared memory */
static REMOTE_CONTROL* hook_smem(void)
{
  int shared_id = shmget(SHARED_MEM_KEY, 
                         sizeof(REMOTE_CONTROL), 
                         IPC_CREAT | S_IRUSR | S_IWUSR);
  return (REMOTE_CONTROL*)shmat(shared_id, 0, 0);
}


/* print help */
static void print_help(void)
{
   printf("Emulator asynchronous debug tool\n");
   printf("     -p              - call the dump statistics\n");
   printf("     -r              - call the reset statistics\n");
   printf("     -f              - force flush of current code cache\n");
}

int main(int argc, char **argv)
{
   /* hook up the shared memory */
   REMOTE_CONTROL* smem=hook_smem();

   /* help ? */
   if (argc==1) print_help();

   /* process all command lines */
   int i=0;
   for(;i<argc;i++)
   {
       if (!strcmp("-p", argv[i]))
       {
          /* call the dump statistics */
          force_dump(smem);
       }
       if (!strcmp("-r", argv[i]))
       {
          /* call the dump statistics */
          force_reset(smem);
       }
   }
   return 0;
}

